package com.pengliu.common.mybatis.page;

import java.io.Serializable;

import org.apache.commons.lang3.StringUtils;


/**
 * @author 邵庆峰
 *         <p>
 *         类描述: 所有分页请求实体父类(分页相关参数)
 *         </p>
 *         <p>
 *         完成日期：2015年12月17日 下午2:51:14
 *         </p>
 */
public class BasePageParams implements Serializable {

	private static final long serialVersionUID = 1L;
    private static final String ASC = "asc";
    private static final String DESC = "desc";

    /**
     * 当前页数
     */
    private Integer current;

    /**
     * 步长(默认为10，如果这个值为0，则查询所有)
     */
    private Integer limit;

    /**
     * 排序规则(默认为""，根据具体业务逻辑使用)
     */
    private String orderBySql = "";

    public String getOrderBySql() {
        return orderBySql;
    }

    public void setOrderBySql(String orderBySql) {
        this.orderBySql = orderBySql;
    }

    public Integer getLimit() {
        if (limit == null) {
        	limit = 10;
        }
        return limit;
    }

    public void setLimit(Integer limit) {
        this.limit = limit;
    }

    public Integer getCurrent() {
        if (null == current || current == 0) {
        	current = 1;
        }
        return current;
    }

    public void setCurrent(Integer current) {
        this.current = current;
    }

    public void addOrder(String orderSQL) {
        if (StringUtils.isEmpty(orderBySql)) {
            orderBySql = orderSQL;
        } else {
            orderBySql += "," + orderSQL;
        }
    }

    public void addOrder(String field, String order) {
        if (!ASC.equalsIgnoreCase(order) && !DESC.equalsIgnoreCase(order)) {
            throw new IllegalArgumentException("order 必须为 asc 或 desc：" + String.valueOf(order));
        }
        addOrder(field + " " + order);
    }
}
